JavaScript syntax
part 19/59 · 107.4 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
s1.valueOf() == s2.valueOf(); // Is true.
Boolean
JavaScript provides a Boolean data type with true and false literals. The typeof operator returns the string "boolean" for these primitive types. When used in a logical context, 0, -0, null, NaN, undefined, and the empty string ("") evaluate as false due to automatic type conversion. All other values (the complement of the previous list) evaluate as true, including the strings "0", "false" and any object.
Type conversion
Automatic type coercion by the equality comparison operators (== and !=) can be avoided by using the type checked comparison operators (=== and !==).
When type conversion is required, JavaScript converts Boolean, Number, String, or Object operands as follows:cite-ref-13[13]
Number and String
The string is converted to a number value. JavaScript attempts to convert the string numeric literal to a Number type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest Number type value.
Boolean
If one of the operands is a Boolean, the Boolean operand is converted to 1 if it is true, or to 0 if it is false.
Object
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────